package com.wy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.pagehelper.PageInfo;
import com.wy.common.Result;
import com.wy.dto.GoodsAuctionDto;
import com.wy.dto.GoodsInfoDto;
import com.wy.entity.GoodsAuction;
import com.wy.entity.GoodsDeposit;
import com.wy.entity.GoodsInfo;
import com.wy.entity.GoodsOrder;
import com.wy.service.GoodsAuctionService;
import com.wy.service.GoodsDepositService;
import com.wy.service.GoodsInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.List;
import java.util.Objects;

/**
 * @Author:王渝
 * @Date: 2023/1/2  15:19
 * @Version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/auction")
public class GoodsAuctionController {

    @Autowired
    private GoodsInfoService goodsInfoService;

    @Autowired
    private GoodsAuctionService goodsAuctionService;

    @Autowired
    private GoodsDepositService goodsDepositService;
    /***
     * 参与拍卖
     * @param goodsInfoDto 商品信息
     * @return
     */
    @RequestMapping("/participate")
    public Result<String> participateAuction(@RequestBody GoodsInfoDto goodsInfoDto){
        if(goodsInfoDto.getUserId().equals(goodsInfoDto.getBuyerUserId())){
            return Result.error("不能参与自己的拍卖");
        }
        LocalDateTime now = LocalDateTime.now();
        if(now.isBefore(goodsInfoDto.getStartingTime())){
            return Result.error("拍卖还没开始");
        }

        if(now.isAfter(goodsInfoDto.getEndTime())){
            return Result.error("拍卖已经结束");
        }

        LambdaQueryWrapper<GoodsDeposit> lambdaQueryWrapper2 =new LambdaQueryWrapper<>();
        lambdaQueryWrapper2.eq(GoodsDeposit::getUserId,goodsInfoDto.getBuyerUserId());
        lambdaQueryWrapper2.eq(GoodsDeposit::getGoodsId,goodsInfoDto.getGoodsId());
        int count = goodsDepositService.count(lambdaQueryWrapper2);
        if(count == 0){
            return Result.error("请先交纳保证金");
        }

        LambdaQueryWrapper<GoodsAuction> lambdaQueryWrapper =  new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(GoodsAuction::getUserId,goodsInfoDto.getBuyerUserId())
                .eq(GoodsAuction::getGoodsId,goodsInfoDto.getGoodsId());
        //查询是否出过价
        GoodsAuction one = goodsAuctionService.getOne(lambdaQueryWrapper);
        GoodsAuction goodsAuction = new GoodsAuction();
        goodsAuction.setBidAmount(goodsInfoDto.getBidAmount());
        goodsAuction.setUserId(goodsInfoDto.getBuyerUserId());
        goodsAuction.setGoodsId(goodsInfoDto.getGoodsId());

        //one != null （出过价）
        if(one != null){
            goodsAuction.setAuctionId(one.getAuctionId());
        }
        boolean b = goodsAuctionService.participateAuction(goodsAuction, one != null);
        if(b){
            return Result.success("出价成功");
        }
        return Result.error("出价失败");

    }

    /**
     * 分页查询用户参与的拍卖
     * @param currentPage 当前页
     * @param pageSize 一页大小
     * @param status 1为进行中 2已结束
     * @param userId 用户id
     * @return
     */
    @RequestMapping("/getAll/{currentPage}/{pageSize}/{status}/{userId}")
    public Result<PageInfo<GoodsAuctionDto>> queryAllInfoByStatusAndUserId(@PathVariable int currentPage,
                                                                           @PathVariable int pageSize,
                                                                           @PathVariable String status,
                                                                           @PathVariable Long userId){
        PageInfo<GoodsAuctionDto> pageInfo = goodsAuctionService.queryPageByUserId(currentPage, pageSize,userId, !"1".equals(status));
        return Result.success(pageInfo);
    }


    /**
     * 查询商品的出价记录
     * @param goodsId 商品id
     * @return
     */
    @RequestMapping("/queryList/{goodsId}")
    public Result<List<GoodsAuction>> queryListByGoodsId(@PathVariable Long goodsId){
        return Result.success(goodsAuctionService.queryListByGoodsId(goodsId));
    }
}
